/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * 修改为外部晶振8M，倍频到168MHZ
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-07-24     RT-Thread    first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include "app_task.h"
#include "drv_common.h"
#include "drv_spi.h"
#include "spi_flash_sfud.h"
#include "dfs_fs.h"

#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>


#define LED0_Pin GET_PIN(F , 9)
#define LED1_Pin GET_PIN(F , 10)
#define SPI1_Cs GET_PIN(B , 14)


int main(void)
{
    int count = 1;
    rt_device_t dev = RT_NULL;
     char buf[] = "hello rt-thread!\r\n";
    __HAL_RCC_GPIOB_CLK_ENABLE();
    rt_pin_mode(LED0_Pin , PIN_MODE_OUTPUT);
    rt_pin_mode(LED1_Pin , PIN_MODE_OUTPUT);
    rt_pin_mode(SPI1_Cs , PIN_MODE_OUTPUT);
    rt_pin_write(SPI1_Cs,PIN_HIGH);
    if(RT_EOK == rt_hw_spi_device_attach("spi1", "spi10", GPIOB, GPIO_PIN_14))  //SPI设备挂载到spi4，设备为spi40,意思是spi4上的0号设备
    {
        rt_kprintf("spi10 attach OK! \r\n");
    }

    if (RT_NULL == rt_sfud_flash_probe("W25Q32", "spi10"))//W25Q64挂载到SPI40设备上。
    {
        return -RT_ERROR;
    }
    if(dfs_mount("W25Q32", "/", "elm", 0, 0) == 0)
    {
        LOG_D("Flash mount OK! \r\n");
    }
    else
    {
        dfs_mkfs("elm","W25Q32");
        rt_kprintf("Flash mount Fail! \r\n");
    }
    dev = rt_device_find("usbd");
    if (dev)
        rt_device_open(dev, RT_DEVICE_FLAG_RDWR);
    else
        return -RT_ERROR;
    while (count++)
    {
//        LOG_D("Hello RT-Thread!");
        rt_device_write(dev, 0, buf, rt_strlen(buf));
        rt_thread_mdelay(1000);
        rt_pin_write(LED0_Pin , count % 2);
        rt_pin_write(LED1_Pin , count % 2);
    }

//    thread_sample();
    return RT_EOK;
}
